<?php

/**
 * @author      Nguyen Duc Thuan <me@ndthuan.com>
 * @copyright   Copyright (c) 2009 Nguyen Duc Thuan <me@ndthuan.com>
 */
class Fete_View_Helper_FormError extends Fete_View_Helper
{
    protected $_field       = null;
    protected $_header      = '';
    protected $_footer      = '';
    protected $_textBefore  = '';
    protected $_textAfter   = '';
    protected $_messages    = '';
    /**
     *
     * @var Fete_Form
     */
    protected $_form        = null;
    protected $_invalids    = array();

    /**
     *
     * @param string $field
     * @param array $messages
     * @param Fete_Form $form
     * @return Fete_View_Helper_FormError
     */
    public function &formError($field = null, $messages = null, $form = null)
    {
        if (null !== $field) {
            $this->_field = $field;
        }

        if (null !== $messages) {
            $this->_messages = $messages;
        }

        if (null !== $form) {
            $this->_form = $form;
        }

        if (null !== $field && null !== $this->_form) {
            foreach ($this->_form->getValidationStates($field)
                as $rule => $result)
            {
                if (false === $result) {
                    if (!isset($this->_invalids[$field])) {
                        $this->_invalids[$field] = array();
                    }
                    $this->_invalids[$field][] = $rule;
                }
            }
        }

        return $this;
    }

    /**
     *
     * @param Fete_Form $form
     * @return Fete_View_Helper_FormError
     */
    public function &setForm(Fete_Form $form)
    {
        $this->_form = $form;

        return $this;
    }

    /**
     *
     * @param string $header
     * @return Fete_View_Helper_FormError
     */
    public function &setHeader($header)
    {
        $this->_header = $header;

        return $this;
    }

    /**
     *
     * @param string $footer
     * @return Fete_View_Helper_FormError
     */
    public function &setFooter($footer)
    {
        $this->_footer = $footer;

        return $this;
    }

    /**
     *
     * @param string $text
     * @return Fete_View_Helper_FormError
     */
    public function &setBeforeMessage($text)
    {
        $this->_textBefore = $text;

        return $this;
    }

    /**
     *
     * @param string $text
     * @return Fete_View_Helper_FormError
     */
    public function &setAfterMessage($text)
    {
        $this->_textAfter = $text;

        return $this;
    }

    public function __toString()
    {
        ob_start();
        $invalids = isset($this->_invalids[$this->_field])
            ? $this->_invalids[$this->_field] : array();

        if (isset($invalids[0])) {
            echo $this->_header;

            foreach ($invalids as $rule)
            {
                if (isset($this->_messages[$rule])) {
                    echo $this->_textBefore, $this->_messages[$rule]
                    , $this->_textAfter;
                }
            }

            echo $this->_footer;
        }

        return ob_get_clean();
    }
}